import string
from random import choice 

mykey = {
"a":[18,19,36,55,62,76,87],
"b":[48,97],
"c":[23,44,92],
"d":[05,29,66],
"f":[24,45],
"g":[01, 96],
"h":[27,64],
"i":[03,10,34,35,54,56,77,86],
"j":[95],
"k":[67],
"l":[21,40,65],
"m":[33,51,80],
"n":[02,37,38,59,61,69,70],
"o":[11,15,25,41,42,57,85],
"p":[12,50],
"q":[90],
"s":[17,28,63,74,89],
"t":[14,16,30,43,58,73,79,84],
"u":[07,22,91],
"v":[99],
"w":[81,98],
"x":[47],
"y":[49,68],
"z":[46],
"e":[00,06,13,32,52,53,71,72,83,93,94],
"r":[26,39,60,75,88],
".":[82],
",":[04],
"?":[31],
"!":[78],
" ":[20],
}

class HomoPhonicCipher:
    def __init__(self, alphabet = mykey):
        self.keys = list(string.ascii_lowercase)
        self.keys.extend(['.', ',', '?', '!', ' '])
        self.alphabet = alphabet
        self.values = list()
        '''
        for char in self.keys:
            possible = self.alphabet[char]
            tempstring = choice(possible)
            if tempstring < 10:
                tempstring = '0' + str(tempstring)
            self.values.append(str(tempstring))
            
        self.map = dict(zip(self.keys, self.values))
        '''
    def encrypt(self, message):
        string = ''

        for char in message:
            ##print char
            possible = self.alphabet[char]
            tempstring = choice(possible)
            if tempstring < 10:
                tempstring = '0' + str(tempstring)
            string += str(tempstring)
        return string
    
    def decrypt(self, message):
        string = ''
        
        position = 0
        while position<= len(message)-2:            
            items = self.alphabet.items()
            substring = message[position:position+2]
            ##print substring  
            position+=2    
            for item in items:
                word = item[0]
                cipher = item[1]               
                if int(substring) in cipher:
                    string += word  
                                
           ## for value in self.alphabet.items():
           ##     if value == char:
           ##         string += key
        
        return string

